package com.nfit.web.adminuser;

import com.nfit.entity.AdminUser;
import com.nfit.entity.Transactions;
import com.nfit.entity.User;
import com.nfit.entity.vo.ResponeVo;
import com.nfit.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.nfit.entity.vo.ResponeVo.fail;
import static com.nfit.entity.vo.ResponeVo.success;

@RestController
public class AdminUserController {
    @Autowired
    private AdminUserService service;

    @RequestMapping("/back-login")
    public ResponeVo backLogin(@RequestBody AdminUser adminUser) {
        AdminUser loginUser = service.login(adminUser);
        if (loginUser != null) {
            return success(loginUser);
        }
        return fail("用户名或密码错误");
    }

    @RequestMapping("/AdminUser/all")
    public ResponeVo all() {
        List<AdminUser> adminUsers = service.selectAll();
        if (adminUsers == null) {
            return ResponeVo.fail("暂无数据");
        }
        return ResponeVo.success(adminUsers);
    }

    @RequestMapping("AdminUser/insert")
    public ResponeVo insert(@RequestBody AdminUser adminUser) {
        int result = service.insert(adminUser);
        if (result == 0) {
            return ResponeVo.fail("添加失败");
        }
        return ResponeVo.success(adminUser);
    }

    @RequestMapping("AdminUser/selectAdminUserById")
    public ResponeVo selectAdminUserById(String adminId) {
        AdminUser adminUser = service.selectAdminUserById(adminId);
        if (adminUser == null) {
            return ResponeVo.fail("暂无数据");
        }
        return ResponeVo.success(adminUser);
    }

    @RequestMapping("AdminUser/update")
    public ResponeVo updateAdminUser(@RequestBody AdminUser adminUser) {
        int result = service.updateAdminUser(adminUser);
        if (result == 0) {
            return ResponeVo.fail("更新失败");
        }
        return ResponeVo.success(adminUser);
    }

    @RequestMapping("AdminUser/delete")
    public ResponeVo deleteAdminUserById(String adminId) {
        int result = service.deleteAdminUserById(adminId);
        if (result == 0) {
            return ResponeVo.fail("删除失败");
        }
        return ResponeVo.success("删除成功");
    }
}
